package 设计模式.singleton.test2;


public class singleton2 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance==instance2);
        System.out.println("instance的hashcode="+instance.hashCode());
        System.out.println("instance的hashcode2="+instance2.hashCode());
    }
}

class Singleton{

    private  static Singleton singleton;

    private Singleton(){
    }

    public static  Singleton getInstance(){
        if (singleton==null){
            synchronized (Singleton.class){
                singleton=new Singleton();
            }

        }
        return singleton;
    }

    /*public static synchronized Singleton getInstance(){
        if (singleton==null){
            singleton=new Singleton();
        }
        return singleton;
    }*/

    /*public static Singleton getInstance(){
        if (singleton==null){
            singleton=new Singleton();
        }
        return singleton;
    }*/
}